package com.example.zrq1010110087.graceplayer.data;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.widget.ArrayAdapter;

import java.util.ArrayList;

/**
 * Created by zrq1010110087 on 2017/10/11.
 */

public class Sdcard_MusicList {

    private static ArrayList<Music> musicArrayList = new ArrayList<Music>();

    private Sdcard_MusicList() {

    }

    public static ArrayList<Music> getMusicArrayList(Context context) {

        if (musicArrayList.isEmpty()) {
            //调用
            Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.TITLE);
            if (cursor != null) {
                int muscicTitle = cursor.getColumnIndex("title");
                int muscicArtist = cursor.getColumnIndex("artist");
                int muscicTotalTime = cursor.getColumnIndex("duration");
                int muscicPath = cursor.getColumnIndex("_data");
                cursor.moveToFirst();
                while (!cursor.isAfterLast()) {

                    String title = cursor.getString(muscicTitle);
                    String artist = cursor.getString(muscicArtist);
                    int totalTime = cursor.getInt(muscicTotalTime);
                    String path = cursor.getString(muscicPath);
                    Music music = new Music(title, artist, path, totalTime);
                    musicArrayList.add(music);
                    cursor.moveToNext();
                }

            }
        }
        return musicArrayList;

    }

}
